package com.dingreading.cloud.web.service.impl;

import com.dingreading.cloud.web.dto.ReadstarStoreDto;
import com.dingreading.cloud.web.dto.ReadstarStoreDto2;
import com.dingreading.cloud.web.entity.ReadstarStore;
import com.dingreading.cloud.web.entity.table.ReadstarStoreTableDef;
import com.dingreading.cloud.web.mapper.ReadstarStoreMapper;
import com.dingreading.cloud.web.service.ReadstarStoreService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.dingreading.cloud.web.entity.table.StoresTableDef.stores;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2025-03-24
 */
@Service
public class ReadstarStoreServiceImpl extends BaseServiceImpl<ReadstarStoreMapper, ReadstarStore, ReadstarStoreTableDef> implements ReadstarStoreService {

    @Override
    protected ReadstarStoreTableDef getTable() {
        return ReadstarStoreTableDef.readstarStore;
    }

    @Override
    public List<ReadstarStoreDto> listByProvince(String province, String city, String county) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(stores.province.eq(province))
                .and(stores.activated.eq(1))
                .and(stores.enabled.eq(1))
                .and(table.enabled.eq(1))
                .and(table.sign.eq(1));
        if (StringUtils.isNotBlank(city))
            condition.and(stores.city.eq(city));
        if (StringUtils.isNotBlank(county))
            condition.and(stores.county.eq(county));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.storeUid, table.storeName, stores.address, table.phone)
                .from(table)
                .leftJoin(stores).on(table.storeUid.eq(stores.uid))
                .where(condition);
        return getMapper().selectListByQueryAs(wrapper, ReadstarStoreDto.class);
    }

    @Override
    public List<ReadstarStoreDto2> storeNameList() {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.storeName, stores.lat, stores.lng, table.phone)
                .from(table)
                .leftJoin(stores).on(table.storeUid.eq(stores.uid))
                .orderBy(stores.id.asc());
        return getMapper().selectListByQueryAs(wrapper, ReadstarStoreDto2.class);
    }

}
